package com.socket;

import java.io.BufferedWriter;
import java.io.IOException;

import com.qy.util.LogUtil;

public abstract class AdminCommand {

	protected void send(BufferedWriter writer, String str) {
		try {
			writer.write(str);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			LogUtil.error("send error", e);
			e.printStackTrace();
		}
	}

	public void execute(String[] params, BufferedWriter writer) {
		AdminCmd cmd = this.getClass().getAnnotation(AdminCmd.class);
		try {
			if (!cmd.paramCounts().contains(String.valueOf(params.length))) {
				send(writer, "参数不正确" + cmd.desc());
				return;
			}
			if (!exec(params, writer)) {
				send(writer, "参数不正确" + cmd.desc());
			}
		} catch (Exception e) {
			send(writer, cmd.desc());
			LogUtil.error("cmd:" + cmd.code(), e);
		}
	}

	public abstract boolean exec(String[] params, BufferedWriter writer);
}
